cap log close
log using "${logpath}case_counts.log", replace

/*******************************************************************************
case_counts.do

This code reports the number of firms subject to litigation
*******************************************************************************/

qui {
	import excel "${rawdatapath}CaseData.xlsx", sheet("Dialysis Cases Final") firstrow clear
	drop if missing(CaseFirmName)

	gen dateformat_MDY=strpos(ComplaintorIndictmentDate,"/")>0
	gen date=date(ComplaintorIndictmentDate,"MDY") if dateformat_MDY==1
	replace date=date(ComplaintorIndictmentDate,"DMY") if missing(date)
	format date %td

	gen firmnames=subinstr(CaseFirmName,", Inc.", " Inc.",.)
	replace firmnames=subinstr(firmnames,", Ltd.", " Ltd.",.)
	replace firmnames=subinstr(firmnames,", LLC", " LLC",.)
	replace firmnames=subinstr(firmnames,", LP", " LP",.)
	split firmnames, parse(, " and ")

	gen NPI=FirmNPI
	foreach char in "q" "w" "e" "r" "t" "y" "u" "i" "o" "p" "a" "s" "d" "f" "g" "h" "j" "k" "l" "z" "x" "c" "v" "b" "n" "m" "Q" "W" "E" "R" "T" "Y" "U" "I" "O" "P" "A" "S" "D" "F" "G" "H" "J" "K" "L" "Z" "X" "C" "V" "B" "N" "M" "-" "." "/" ":" "'" {
		replace NPI=subinstr(NPI,"`char'","",.)
	}

	split NPI, parse(, " " ;) destring

	gen NPIfirms=0
	forvalues i=1/69 {
		replace NPIfirms=NPIfirms+1 if !missing(NPI`i')
	}

	gen namefirms=0
	forvalues i=1/17 {
		replace namefirms=namefirms+1 if !missing(firmnames`i')
	}

	bysort DocketDistrict CriminalvsCivil: egen first_treat=min(date)
	gen in2years=date<=first_treat+365*2

	keep if date>=dofy(2007) & date<dofy(2015)
}

bysort CriminalvsCivil: gen cases=_N
bysort CriminalvsCivil: egen totalnames=total(namefirms)
bysort CriminalvsCivil: egen totalNPIs=total(NPIfirms)

list CriminalvsCivil cases totalnames totalNPIs if _n==1 | _n==_N // Referenced in Section 6.2, Paragraph 1 and Appendix J.2, Bullets 2-3

bysort CriminalvsCivil in2years: egen name2year=total(namefirms)
bysort CriminalvsCivil in2years: egen NPI2year=total(NPIfirms)

list CriminalvsCivil in2years name2year NPI2year if _n==_N // Referenced in Section 5.4, Paragraph 3

log close